package function;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

import UserDatabase.CardDatabase;

public class CardFrame extends JFrame{
	
	private String [] tableHead;
	private String [][] card;
	private JTable table; 
	private JLabel l_id,l_m;
	private JTextField t_id,t_m; 
	private JButton b_confirm,b_reset;
	private String SQL,Username;
	private CardDatabase cd = new CardDatabase();
	
	public CardFrame(String SQL,String Username) {
		this.SQL = SQL;
		this.Username = Username;
		this.setSize(430,350);
		this.setTitle("一卡通充值");
		this.setLocation(500,150);
		init();
		this.setVisible(true);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			
			public void windowClosing(WindowEvent e) {
				new UserFrame(Username);
				setVisible(false);
			}
			
		});
	}
	
	public void init() {
		cd.setSQL(SQL);
		card = cd.getRecord();
		tableHead = cd.getColumnName();
		table = new JTable(card,tableHead);
		Font fontB = new Font("宋体",Font.BOLD,16);
		
		l_id = new JLabel("请输入编号：",JLabel.CENTER);
		l_id.setFont(fontB);
		l_m = new JLabel("充值金额：",JLabel.CENTER);
		l_m.setFont(fontB);
		
		t_id = new JTextField();
		t_id.setFont(fontB);
		t_m = new JTextField();
		t_m.setFont(fontB);
		
		b_confirm = new JButton("确认充值");
		b_confirm.setFont(fontB);
		b_confirm.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(!t_m.getText().equals("")&&!t_id.getText().equals("")) {
					int money = Integer.parseInt(t_m.getText());
					int id = Integer.parseInt(t_id.getText());
				
					cd.addCard(id,money);
					JPanel f =new JPanel();
					JOptionPane.showMessageDialog(f,"充值成功！","提示框",JOptionPane.WARNING_MESSAGE);
					new UserFrame (Username);
					setVisible(false);
				}else {
					JPanel f =new JPanel();
					JOptionPane.showMessageDialog(f,"请完善充值信息！","提示框",JOptionPane.WARNING_MESSAGE);
				}
			}
			
		});
		
		b_reset = new JButton("重置");
		b_reset.setFont(fontB);
		b_reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				t_id.setText("");
				t_m.setText("");
			}
		});
		
		
		JScrollPane sp = new JScrollPane(table);
		sp.setBounds(15, 15, 380, 50);
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(2,2,5,5));
		p.add(l_id);
		p.add(t_id);
		p.add(l_m);
		p.add(t_m);
		p.setBounds(15,135,380,80);
		
		JPanel bp = new JPanel();
		bp.setLayout(new GridLayout(1,2,5,5));
		bp.add(b_confirm);
		bp.add(b_reset);
		bp.setBounds(15, 255, 380, 30);
		
		this.setLayout(null);
		this.add(sp);
		this.add(p);
		this.add(bp);
		
	}
}
